import 'dart:convert';

import 'package:projectfluttershengyihuatong/category/CatetoryModel.dart';
import 'package:projectfluttershengyihuatong/courseDetail/CourseDetailModel.dart';
import 'package:projectfluttershengyihuatong/service/loginService/UserInfoModel.dart';
import 'package:projectfluttershengyihuatong/tab1/HomeModel.dart';

import 'jsonModel.dart';

//工具类
class JSONManager{
  /// 单例对象
  static JSONManager? _instance;

  /// 内部构造方法，可避免外部暴露构造函数，进行实例化
  JSONManager._internal();

  /// 工厂构造方法，这里使用命名构造函数方式进行声明
  factory JSONManager.getInstance() => _getInstance();

  /// 获取单例内部方法
  static _getInstance() {
    // 只能有一个实例
    _instance ??= JSONManager._internal();
    return _instance;
  }

  dynamic fromJson(dynamic content, Type type) {
    var value;
    if (content is String) {
      //  字符串需要经过json解码变为map格式
      value = json.decode(content);
      if (value is List) {
        return  fromJson(value, type);
      }

    }else if (content is Map<String, dynamic>) {
      value = content;
    }else if (content is List) {
      value = content;
    }else {
      throw const FormatException("content is not json format");
    }

    switch (type) {
      case LaunchConfig:
        return LaunchConfig.fromJson(value);
      case UserInfoModel:
        return UserInfoModel.fromJson(value);
      case const (List<CatetoryModel>):
        return (value as List).map<CatetoryModel>((e) {
          return fromJson(e, CatetoryModel);
        }).toList();
      case CatetoryModel:
        return CatetoryModel.fromJson(value);
      case HomeModel:
        return HomeModel.fromJson(value);
      case CourseDetailModel:
        return CourseDetailModel.fromJson(value);


      default:
        throw const FormatException("no model provided for content");
    }



  }
}

class BaseModel<T> {
  int ret;
  String msg;
  T? data;
  BaseModel({this.ret = 0, this.msg = "", this.data});

  factory BaseModel.fromJson(Map<String, dynamic> json) => BaseModel(
  ret: (json["ret"] as int?) ?? 0,
  msg: (json["msg"] as String?) ?? "",
  data:json["data"] == null ? null:JSONManager.getInstance().fromJson(json["data"], T)
  );

}



// case PageEntity<HomeBannerModelEntity, HomeBannerModelEntity>:
//   return PageEntity<HomeBannerModelEntity,HomeBannerModelEntity>.fromJson(value);
// case PageEntity<MyZoneEntity, MyZoneEntity>:
//   return PageEntity<MyZoneEntity,MyZoneEntity>.fromJson(value);
// case PageEntity<LessonDetailOne, LessonDetailOne>:
//   return PageEntity<LessonDetailOne,LessonDetailOne>.fromJson(value);
// case List<HomeBannerModelEntity>:
//   return (value as List).map<HomeBannerModelEntity>((e){
//     return fromJson(e,HomeBannerModelEntity);
//   }).toList();
// case HomeBannerModelEntity:
//   return HomeBannerModelEntity.fromJson(value);
// case BaseModel<List<HomeActiveEntity>>:
//   return BaseModel<List<HomeActiveEntity>>.fromJson(value);
// case List<HomeActiveEntity>:
//   return (value as List).map<HomeActiveEntity>((e){
//     return fromJson(e, HomeActiveEntity);
//   }).toList();
// case List<LessonDetailOne>:
//   return (value as List).map<LessonDetailOne>((e){
//     return fromJson(e, LessonDetailOne);
//   }).toList();
// case List<MyZoneEntity>:
//   return (value as List).map<MyZoneEntity>((e){
//     return fromJson(e, MyZoneEntity);
//   }).toList();
// case TaskDetailEntity:
//   return TaskDetailEntity.fromJson(value);
// case TaskFinishedDetailEntity:
//   return TaskFinishedDetailEntity.fromJson(value);
//
// case HomeActiveEntity:
//   return HomeActiveEntity.fromJson(value);
// case LessonDetailOne:
//   return LessonDetailOne.fromJson(value);
// case LessonDetailEntity:
//   return LessonDetailEntity.fromJson(value);
// case MyZoneEntity:
//   return MyZoneEntity.fromJson(value);
// case ProfileModelEntity:
//   return ProfileModelEntity.fromJson(value);
// case ProfileJiFenEntity:
//   return ProfileJiFenEntity.fromJson(value);
// case List<LaborLessonClassEntity>:
//   return (value as List).map<LaborLessonClassEntity>((e){
//     return fromJson(e, LaborLessonClassEntity);
//   }).toList();
// case LaborLessonClassEntity:
//   return LaborLessonClassEntity.fromJson(value);






class PageEntity<T, M> {

  List<T>? orders;
  int? pages;
  int? current;
  int? size;
  bool? hitCount;
  bool? searchCount;
  int? total;
  List<M>? records;

  PageEntity({this.orders, this.pages, this.current, this.size, this.hitCount, this.searchCount, this.total,  this.records});

  factory PageEntity.fromJson(Map<String, dynamic> json) => PageEntity(
    orders: json["orders"] == null ? null:JSONManager.getInstance().fromJson(json["orders"], List<T>),
    pages:json["pages"] as int?,
    current: json["current"] as int?,
    size: json["size"] as int?,
    hitCount: json["hitCount"] as bool?,
    searchCount: json["searchCount"] as bool?,
    total: json["total"] as int?,
    records: json["records"] == null ? null:JSONManager.getInstance().fromJson(json["records"], List<M>),


  );

}